package com.qf.java2201.controller;

import com.qf.java2201.common.PageVo;
import com.qf.java2201.common.R;
import com.qf.java2201.pojo.Users;
import com.qf.java2201.service.UsersService;
import com.qf.java2201.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
//@CrossOrigin("*")
public class UsersController {
    @Autowired
    private UsersService usersService;

    @RequestMapping("/usersList")
    public PageVo<Users> getPageDate(@RequestParam Map<String,Object> params){
        return usersService.getPageDate(params);
    }

    @RequestMapping("/addUsers")
    public R addusers(Users users){
        //解决与前端交互日期转换问题
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        users.setUtime(sdf.format(new Date()));

        R r = new R();
        int result = usersService.addUsers(users);

        if(result==1){
            r.setCode(0);
            r.setMsg("添加成功");
        }else{
            r.setCode(-1);
            r.setMsg("添加失败");
        }
        return r;
    }

    @RequestMapping("/deleteUsers")
    public R deleteUsersById(Integer user_id){
        R r = new R();

        int result = usersService.deleteUsersById(user_id);

        if (result==1){
            r.setCode(0);
            r.setData(result);
            r.setMsg("删除成功");
        }else {
            r.setCode(-1);
            r.setMsg("删除失败");
        }

        return r;
    }


    @RequestMapping("/updateUsers")
    public R updateUsers(Users users){
        R r = new R();

        int result = usersService.updateUsersById(users);

        if (result==1){
            r.setCode(0);
            r.setData(result);
            r.setMsg("修改成功");
        }else {
            r.setCode(-1);
            r.setMsg("修改失败");
        }

        return r;
    }

    /*模糊查询*/

    @RequestMapping("/selectUsersCond")
    public R selectUsersCond(Users users){
        R r = new R();

        List<Users> list = usersService.selectUsersCond(users);

        if (list != null){
            r.setCode(0);
            r.setData(list);
            r.setMsg("查询成功");
        }else {
            r.setCode(-1);
            r.setMsg("查询失败");
        }
        return r;
    }

    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        R r = new R();

        String filename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();

        String phth = OssUtil.uploadFile(inputStream, filename);

        r.setCode(0);
        r.setData(phth);
        r.setMsg("上传成功");

        return r;
    }


}
